package com.ecjtu.stadiumre_servations.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.ecjtu.stadiumre_servations.config.RabbitMQConfig;
import com.ecjtu.stadiumre_servations.entity.domain.GroupActivity;
import com.ecjtu.stadiumre_servations.entity.domain.GroupMember;
import com.ecjtu.stadiumre_servations.entity.domain.GroupRecord;
import com.ecjtu.stadiumre_servations.mapper.BookingMapper;
import com.ecjtu.stadiumre_servations.mapper.GroupActivityMapper;
import com.ecjtu.stadiumre_servations.mapper.GroupMemberMapper;
import com.ecjtu.stadiumre_servations.mapper.GroupRecordMapper;
import com.ecjtu.stadiumre_servations.service.AlipayService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class GroupActivityMessageListener {
    @Resource
    private GroupActivityMapper groupActivityMapper;
    @Resource
    private GroupRecordMapper groupRecordMapper;
    @Resource
    private GroupMemberMapper groupMemberMapper;
    @Resource
    private BookingMapper bookingMapper;
    @Resource
    private AlipayService alipayService;

    @RabbitListener(queues = RabbitMQConfig.GROUP_ACTIVITY_QUEUE)
    public void handleMessage(Map<String, String> message) {
        String activityId = message.get("activityId");
        String bookingId = message.get("bookingId");
        GroupActivity activity = groupActivityMapper.selectById(activityId);
        if (activity != null && "拼团中".equals(activity.getActivityStatus())) {
            // 删除拼团活动
            activity.setActivityStatus("拼团失败");
            groupActivityMapper.updateById(activity);
//            groupActivityMapper.deleteById(activityId);
            // 删除相关拼团记录
            LambdaQueryWrapper<GroupRecord> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(GroupRecord::getActivityId, activityId);
            List<GroupRecord> groupRecordList = groupRecordMapper.selectList(lambdaQueryWrapper);

            // 自动退款给所有拼团用户
            for (GroupRecord groupRecord : groupRecordList) {
                LambdaQueryWrapper<GroupMember> groupMemberLambdaQueryWrapper = new LambdaQueryWrapper<>();
                groupMemberLambdaQueryWrapper.eq(GroupMember::getGroupRecordId, groupRecord.getRecordId());
                List<GroupMember> groupMembers = groupMemberMapper.selectList(groupMemberLambdaQueryWrapper);
                for (GroupMember groupMember : groupMembers) {
                    if ("已支付".equals(groupMember.getPayStatus())) {
                        Map<String, String> refundParams = new HashMap<>();
                        refundParams.put("out_trade_no", groupMember.getMemberId()); // 使用成员ID作为外部交易号
                        refundParams.put("refund_amount", String.format("%.2f", activity.getActivityPrice())); // 退款金额
                        try {
                            alipayService.refund(refundParams);
                        } catch (Exception e) {
                            e.printStackTrace();
                            // 处理退款失败逻辑
                        }
                    }
                }
            }
            // 删除相关的拼团人员记录表
//            for (GroupRecord groupRecord : groupRecordList) {
//                LambdaQueryWrapper<GroupMember> groupMemberLambdaQueryWrapper = new LambdaQueryWrapper<>();
//                groupMemberLambdaQueryWrapper.eq(GroupMember::getGroupRecordId, groupRecord.getRecordId());
//                groupMemberMapper.delete(groupMemberLambdaQueryWrapper);
//            }
//            groupRecordMapper.deleteBatchIds(groupRecordList);
            bookingMapper.deleteById(bookingId);
        }
    }
}